// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
       scrollView: cc.ScrollView,
       itemTemplate: { // item template to instantiate other items
            default: null,
            type: cc.Prefab
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        // 初始化数据
        this.list = [
            1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63
        ];
        // 初始化滚动组件内容区域
        this.content = this.scrollView.content;
        // 初始化预制体
        this.initialize();
        this.scrollView.node.on('scroll-to-top', this.onScrollToTop, this);
        this.scrollView.node.on('scroll-to-bottom', this.onScrollToBottom, this);
    },
    initialize(){
        // 计算内容区域的高度
        this.content.height = 10 * (this.itemTemplate.height + 10) + 10;
        for(let i=0; i< this.list.length -1; i++){
            let item = cc.instantiate(this.itemTemplate);
            // item.position.y = i * (item.height + 10);
            // item.setPosition(item.x, -i * (item.height + 10));
            // 设置item的属性
            item.getComponent("listItem").initItem(this.list[i]);
            // 将item添加到内容区域中
            this.content.addChild(item);
        }
    },

    onScrollToTop () {

    },
    onScrollToBottom(){

    }

    // update (dt) {},
});
